package com.fingard.dsp.bank.directbank.sjcb01;

import java.io.File;
import java.io.IOException;

import com.fingard.dsp.bank.directConfig.FtpSet;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ConnState;
import com.fingard.net.FtpReceiver;
import com.fingard.net.SftpPortClient;
import com.fingard.text.StringHelper;

/**
 * @author XueJy on 2018/8/31.
 * @version 1.0
 */
public class SJCB01Base extends DirectBase {

    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }


    String[] sendToBank(String recFileBakPath, String recFileName) throws Exception {

        SftpPortClient sftpPortClient = null;
        try {
            FtpSet ftpSet = bankFront.getFtpSet("batchpay");
            sftpPortClient = new SftpPortClient(ftpSet);

            String trgFilePath = StringHelper.trimEnd(ftpSet.upDir, "/") + "/" + recFileName;

            WriteBankLogLn2("SFTP服务器文件上传路径:" + trgFilePath);

            FtpReceiver ftpReceiver = sftpPortClient.uploadFile("", recFileBakPath, trgFilePath);

            if (ftpReceiver.isCompleted) {
                return new String[]{"0"};
            } else if (ConnState.FAIL.equals(ftpReceiver.connState)) {
                return new String[]{"-1",ftpReceiver.connState};
            } else {
                return new String[]{"-2", ftpReceiver.connMsg};
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (sftpPortClient != null) {
                try {
                    sftpPortClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                    WriteBankLogLn(e);
                }
            }
        }
    }

    FtpReceiver downloadFromFtp(String fileName, String localPath) throws Exception {
        FtpSet ftpSet = bankFront.getFtpSet("batchpay");
        SftpPortClient sftpPortClient = null;
        try {
            sftpPortClient = new SftpPortClient(ftpSet);
            String failDownPath = fileName + "_fail";
            String successDownPath =   fileName + "_success";

            WriteBankLogLn2("失败文件下载路径:" + StringHelper.trimEnd(ftpSet.downDir, "/") +"/" +failDownPath);

            WriteBankLogLn2("成功文件下载路径:" + StringHelper.trimEnd(ftpSet.downDir, "/") +"/" +successDownPath);

            File resultdir = new File(localPath);
            if (!resultdir.exists()) {
                resultdir.mkdirs();
            }
            //下载失败结果文件
            FtpReceiver failReceiver= sftpPortClient.downloadFile("", failDownPath,StringHelper.trimEnd(localPath, File.separator));

            //下载成功结果文件
            FtpReceiver sucReceiver = sftpPortClient.downloadFile("", successDownPath, StringHelper.trimEnd(localPath, File.separator));
            if (failReceiver.isCompleted) {
                return failReceiver;
            }
            return sucReceiver;
        } catch (Exception e) {
            throw e;
        } finally {
            if (sftpPortClient != null) {
                try {
                    sftpPortClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                    WriteBankLogLn(e);
                }
            }
        }
    }

}
